package com.infore.statisticsAndAlarm.select.filter.condition;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import com.infore.statisticsAndAlarm.select.chain.ConditionFilterChain;
import com.infore.statisticsAndAlarm.select.enums.OperatorEnum;
import com.infore.statisticsAndAlarm.select.filter.ConditionFilter;
import com.infore.statisticsAndAlarm.select.util.QueryUtil;

/**
 * 小于等于过滤器
 */
public class LeFilter implements ConditionFilter {
	
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public void doFilter(List<Predicate> predicates, CriteriaBuilder criteriaBuilder, Root<?> root, String property,
			String operator, String content, ConditionFilterChain chain,boolean interrupt) {
		if(OperatorEnum.LE.getOperator().equals(operator)) {
			Path path = QueryUtil.getPropertyPath(property, root);
			if (content.contains("-") || content.contains(":")) {
				try {
					predicates.add(criteriaBuilder.lessThanOrEqualTo(path,
							new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(content)));
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else {
				predicates.add(criteriaBuilder.le(path, Double.parseDouble(content)));
			}
			interrupt = true;
		}
		chain.doFilter(predicates,criteriaBuilder,root,property,operator,content,chain,interrupt);	
	}
}
